//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft shared
// source or premium shared source license agreement under which you licensed
// this source code. If you did not accept the terms of the license agreement,
// you are not authorized to use this source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the SOURCE.RTF on your install media or the root of your tools installation.
// THE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

Module Name:    LOG2PHYSMAP.H

Abstract:       Logical --> Physical Sector Mapper module

note: add one bit to indicate if this sector is deleted  
-----------------------------------------------------------------------------*/

#ifndef _LOG2PHYSMAP_H_
#define _LOG2PHYSMAP_H_ 

#include <windows.h>
#include "falmain.h"

//---------------------------- Macro Definitions -----------------------------
#define  MASTER_TABLE_SIZE      256

//------------------------------- Public Interface ------------------------------
class MappingTable
{
public:
    MappingTable(DWORD dwStartLogSector, DWORD dwStartPhysSector);
    BOOL Init(PFlashRegion pRegion);
    BOOL Deinit();
     
    PBYTE GetPhysicalSectorAddr(SECTOR_ADDR logicalSectorAddr, PSECTOR_ADDR pPhysicalSectorAddr);
    PBYTE MapLogicalSector(SECTOR_ADDR logicalSectorAddr, SECTOR_ADDR physicalSectorAddr, 
                           PSECTOR_ADDR pExistingPhysicalSectorAddr);

    BOOL  IsValidLogicalSector(SECTOR_ADDR logicalSectorAddr );

public:
    SECTOR_ADDR GetLogicalSectAddr(SECTOR_ADDR physicalSectAddr);
    DWORD GetMappedNumb(){ return m_dwNumbMaped;};
#ifdef DEBUG
#define DumpMappingTable(cond, pMappingTable) if(cond){ pMappingTable->Dump();}
    VOID Dump();
#else
#define DumpMappingTable(cond, pMappingTable);
#endif

private:
    DWORD*   m_pDynamicLUT[MASTER_TABLE_SIZE]; // Dynamic look-up table (DLUT) that stores logical --> physical
                                               // sector mapping information (see above)

    DWORD    m_dwNumSectorsPerSecTable;        // Number of sectors mapped in each secondary table

    DWORD    m_dwStartLogSector;
    DWORD    m_dwNumLogSectors;
    DWORD    m_dwStartPhysSector;
    DWORD    m_dwNumPhysSectors;

    DWORD m_dwNumbMaped;

};
//------------------------------ Helper Functions ------------------------------

#endif _LOG2PHYSMAP_H_
